//
//  LPJoinPersonListViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/7/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPJoinPersonListViewController.h"
#import "YBPopupMenu.h"
#import "LPJoinPersonListModel.h"
#import "LPJoinPersonListTableViewCell.h"

#define CELLID @"LPJoinPersonListTableViewCell"
@interface LPJoinPersonListViewController ()<UITableViewDelegate,UITableViewDataSource,YBPopupMenuDelegate,LPJoinPersonListTableViewAllographDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  page  */
@property (assign, nonatomic) NSInteger  page;

/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  判断筛选的类型  */
@property (strong, nonatomic) NSString  *shaixuan;

@end

@implementation LPJoinPersonListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"参与人数";
    self.rightButton.hidden = NO;
    [self.rightButton setTitle:@"筛选" forState:UIControlStateNormal];
    self.shaixuan = @"0";
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.model.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"10";
    paramer[@"STATUS"] = self.shaixuan;
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActEnrollList] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ENROLL_SIGN_LEAVE"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPJoinPersonListModel class] json:array] mutableCopy];
            
        }else{
            [weakSelf.dataSouce removeAllObjects];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showNoDataWithView:weakSelf.tableView];
        }
    } Fail:^(id object) {
         [weakSelf.dataSouce removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.model.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"10";
    paramer[@"STATUS"] = self.shaixuan;
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActEnrollList] Parameter:paramer Success:^(id object) {
        
        NetWorkModel *mode = [NetWorkModel yy_modelWithJSON:object];
        if ([mode.STATE isEqualToString:@"20000"]) {
            
            NSArray *array = [object objectForKey:@"ENROLL_SIGN_LEAVE"];
            if (array.count == 0) {
                [YJProgressHUD showMsgWithoutView:@"没有数据"];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
            }
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPJoinPersonListModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        
    }];
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    
}


#pragma mark - LPJoinPersonListTableViewAllographDelegate
- (void)LPJoinPersonListTableViewAllograph_index:(NSInteger)index isApply:(BOOL)isApply{
    if (isApply) {//催报
        [self netWorking_cuiBao_index:index];
    }else{//代签
        [self netWorking_daiQian_index:index];
    }
}
///代签
- (void)netWorking_daiQian_index:(NSInteger)index{
    __weak typeof(self) weakSelf = self;
    LPJoinPersonListModel *choose = self.dataSouce[index];
    NSString *userid = [NSString isNilStringReturnString:choose.USER_ID];
    NSArray *personInfor = @[@{@"AI_ID": self.model.ID, @"USER_ID": userid, @"NAME": choose.USER_NAME, @"PMID": choose.PMID, @"ACCOUNTID": choose.ACCOUNTID}];
    /**  代签  */
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"ENROLL"] = personInfor;
    [YJProgressHUD showMessage:@"代签中..." duration:10 view:self.view];
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionAllograph] Parameter:paramer Success:^(id object) {
        NetWorkModel *status = [NetWorkModel yy_modelWithJSON:object];
        if ([status.STATE isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"代签成功"];
            choose.SIGN = @"1";
            weakSelf.dataSouce[index] = choose;
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
            [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];
        }else{
            [YJProgressHUD showMsgWithoutView:@"代签失败"];
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"代签失败"];
    }];
}

///催报
- (void)netWorking_cuiBao_index:(NSInteger)index{
    [YJProgressHUD showMessage:@"催报中..." duration:10 view:self.view];
    NSArray *LIST = @[ZJUSERINFORMODEL.ORG_ACCOUNT.ID];
    NSString *CONTENT = [NSString stringWithFormat:@"%@活动将于%@举行, 请立即报名！",self.model.NAME,self.model.START_TIME];
    NSDictionary *dict = @{@"AI_ID":self.model.ID,
                           @"CONTENT":CONTENT,
                           @"ACT_NAME":self.model.NAME,
                           @"PUSH_MSG":@"1",
                           @"PUSH_MSG":@"1",
                           @"LIST":LIST};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactEnrollSendMsg] Parameter:dict Success:^(id object) {
        NetWorkModel *status = [NetWorkModel yy_modelWithJSON:object];
        if ([status.STATE isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"催报成功"];
        
        }else{
            [YJProgressHUD showMsgWithoutView:@"催报失败"];
        }

    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"催报失败"];
    }];
}
#pragma mark - 监听事件
- (void)action_rightButton:(UIButton *)sender{
    [YBPopupMenu showRelyOnView:self.rightButton titles:@[@"所有", @"未报名", @"已报名", @"已签到", @"已请假"] icons:@[] menuWidth:100 * [UIScreen zj_screenWScale] showCount:0 delegate:self];
}

#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    self.shaixuan = [NSString stringWithFormat:@"%ld",(long)index];
    [self.tableView.mj_header beginRefreshing];

}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    LPJoinPersonListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];

    cell.model = self.dataSouce[indexPath.row];
    cell.delegate = self;
    cell.row = indexPath.row;
    return cell;
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        [_tableView registerClass:[LPJoinPersonListTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (_dataSouce == nil) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
